<?php


class Tools extends CI_Controller
{

    public function message($to = 'World')
    {
        echo "Hello {$to}!" . PHP_EOL;
    }

    public function sendjsonmsg($row=array()){
        if(empty($row['code'])){
            $row['code']=0;
        }
        if(empty($row['msg'])){
            $row['msg']="无消息";
        }
        echo json_encode($row);   exit;
    }

    /**
     * $type :推送类型 1京东  2 拼多多
     * 推送任务至任务池中
     */
    public function  sendtask($type=1){
        $this->load->model('Jd_task_jd_model');
        $row=$this->Jd_task_jd_model->getwaittask();
        if(empty($row)){
            $msg['code']=1;
            $msg['msg']="没有待推送的任务";
            $this->sendjsonmsg($msg);
        }
        $this->load->model('Jd_task_jd_list_model');
        $this->load->model('Jd_task_jd_list_pool_model');
        $idlist=array();
        $taskidstr=array();
        $this->db->trans_begin();



        $sendnubjd=0;
        $sendnubpdd=0;

        foreach ($row as  $key=>$value){
            $taskid=$value['id'];
            $interval=$value['interval'];
            $lastsendtime=$value['lastsendtime']?$value['lastsendtime']:0;
            $realinterval=intval(time()-$lastsendtime)/60;


            if($realinterval<$interval ){
                continue;
            }
            $listrow= $this->Jd_task_jd_list_model->getminlitstake($taskid);
            if(empty($listrow)){
                continue;
            }
            $listrow['listid']=$listrow['id'];


            unset($listrow['id']);
            $listrow['addtime']=time();
            unset($listrow['hunter_id']);
            unset($listrow['hunter_name']);
            unset($listrow['team']);
            unset($listrow['update']);
            $listrow['shop_name']=$value['shop_name'];
            $r=$this->Jd_task_jd_list_pool_model->addtasklisttopoll($listrow);
            if($r){
                $idlist[]=$listrow['listid'];
                $taskidstr[]=$taskid;
                if($listrow['type']==1){
                    $sendnubjd++;
                }
                if($listrow['type']==2){
                    $sendnubpdd++;
                }

            }
        }


        if(!empty($idlist)){
            $result= $this->Jd_task_jd_list_model->updatapool($idlist);
            if($result){
                $result=$this->Jd_task_jd_model->UpdatelastSendTime($taskidstr);
                if($result){
                    $this->db->trans_commit();
                     $redis=$this->MyRedis();
                     $redis->update('poolnub1',$sendnubjd);
                     $redis->update('poolnub2',$sendnubpdd);

                    $msg['code']=0;
                    $msg['msg']="推送成功";
                    $this->sendjsonmsg($msg);
                }
            }
        }else{
            $msg['code']=1;
            $msg['msg']="没有合适任务或推送失败";
            echo "没有合适任务或推送失败"; exit;
            $this->sendjsonmsg($msg);
        }
        echo "成功"; exit;
    }



    /**
     *
     * 计算商户所有余额状态
     */
    public function totalbusiness(){
        $now=time();
        $yestodaydate=date("Y-m-d",strtotime("-1 day"));
        $yestoday=strtotime($yestodaydate); //昨天凌晨
        $this->load->model('Jd_task_jd_list_model');
        $row= $this->Jd_task_jd_list_model->totalbunissmoney($yestoday);
        $this->load->model('Jd_buniess_balance_model');
        $this->Jd_buniess_balance_model->addlog($row,$yestodaydate);
        $this->load->model('Jd_task_jd_list_pool_model');
        $this->Jd_task_jd_list_pool_model->truncatelog();
        echo "完成";
    }


    public function  MyRedis($type=0){
        $config= $this->config->item('redis');
        require_once(FCPATH.'application/libraries/MYredis.php');
        return MYredis::getInstance($config,$type);
    }




    public  function  Resetpoolsnumber(){
        $this->load->model('Jd_task_jd_list_pool_model');
        $result=$this->Jd_task_jd_list_pool_model->getpoolsnumber();
        $redis=$this->MyRedis();

        $arr[1]=1;
        $arr[2]=2;
        foreach ($result as $id=>$v){
            $redis->set('poolnub'.$v['type'],$v['num']);
            unset($arr[$v['type']]);
        }
        if(!empty($arr)){
            foreach ($arr as $id=>$v){
                $redis->set('poolnub'.$id,0);
            }
        }
        echo   $redis->get('poolnub1')."__". $redis->get('poolnub2'); exit;
    }


}